import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_scan_tool/bloc/tool/tool_event.dart';
import 'package:flutter_scan_tool/models/app_theme.dart';
import 'package:flutter_scan_tool/models/tool_kit.dart';
import 'package:select_form_field/select_form_field.dart';

import '../bloc/device/device_bloc.dart';
import '../bloc/tool/tool_bloc.dart';

class SearchDiag extends StatefulWidget {
  const SearchDiag({Key? key}) : super(key: key);

  @override
  _SearchDiagState createState() => _SearchDiagState();
}

class _SearchDiagState extends State<SearchDiag> {
  final _toolName = TextEditingController();
  final _storageName = TextEditingController();
  List<Map<String, dynamic>> list = [];
  List<ToolKit> itemList=[];

  @override
  void initState() {
    super.initState();
    DeviceBloc bloc = context.read<DeviceBloc>();
    itemList = bloc.storageInfo?.toolKitList??[];
    if (itemList.isNotEmpty) {
      for (var item in itemList) {
        list.add({'label': item.toolKitName, 'value': item.id});
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return AlertDialog(
      backgroundColor: Colors.white,
      title: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: const <Widget>[Text('搜索'), CloseButton()],
      ),
      content: Container(
        height: size.height * 0.4,
        child: Column(
          children: [
            Container(
              width: size.width * 0.8,
              decoration: BoxDecoration(
                  shape: BoxShape.rectangle,
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  color: AppTheme.appTheme.cardBackgroundColor(),
                  border: Border.all(color: Colors.grey)),
              padding: const EdgeInsets.only(left: 5),
              child: TextField(
                controller: _toolName,
                decoration: const InputDecoration(
                    icon: Icon(
                      Icons.person,
                      color: Color(0xff6f35a5),
                    ),
                    hintText: '请输入工具名称',
                    border: InputBorder.none),
              ),
            ),
            Container(
              decoration: BoxDecoration(
                  shape: BoxShape.rectangle,
                  borderRadius: const BorderRadius.all(Radius.circular(10)),
                  color: AppTheme.appTheme.cardBackgroundColor(),
                  border: Border.all(color: Colors.grey)),
              margin: const EdgeInsets.only(top: 10),
              padding: const EdgeInsets.only(left: 5),
              child: SelectFormField(
                controller: _storageName,
                type: SelectFormFieldType.dropdown,
                decoration: const InputDecoration(
                    icon: Icon(
                      Icons.select_all_sharp,
                      color: Color(0xff6f35a5),
                    ),
                    border: InputBorder.none,
                    hintText: '请选择工具柜'),
                items: list,
              ),
            )
          ],
        ),
      ),
      actions: [
        IconButton(
            onPressed: () {
              Map<String, dynamic> map = {};
              if (_toolName.text.isNotEmpty) {
                map.putIfAbsent('name', () => _toolName.text);
              }
              if (_storageName.text.isNotEmpty) {
                map.putIfAbsent('toolKits', () => _storageName.text);
              }
              context.read<ToolBloc>().add(ToolLoadEvent(map));
              Navigator.pop(context);
            },
            icon: const Icon(Icons.search))
      ],
    );
  }
}
